import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/home/index'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [{
      path: 'index',
      component: () => import('../views/Index.vue')
    }, {
      path: 'ash',
      component: () => import('../views/zhuanlan/aisonghua.vue')
    }, 
    {
      path: 'eassay',
      component: () => import('../views/eassay/eassay.vue'),
      meta:{
        crumb:['文章管理']
      }
    },
    {
      path: 'eassayUpdate/:id',
      component: () => import('../views/eassay/eassayUpdate.vue'),
      meta:{
        crumb:['文章管理:/home/eassay','文章修改']
      }
    },
    {
      path: 'eassayAdd',
      component: () => import('../views/eassay/eassayAdd.vue'),
      meta:{
        crumb:['文章管理:/home/eassay','文章添加']
      }
    }, 
    {
      path: 'huayi',
      component: () => import('../views/zhuanlan/xifang.vue')
    }, {
      path: 'caoshu',
      component: () => import('../views/shufa/caoshu.vue')
    }, {
      path: 'kaishu',
      component: () => import('../views/shufa/kaishu.vue')
    }, {
      path: 'tuijian',
      component: () => import('../views/tuijian/tuijian.vue')
    },{
      path: 'huayitong',
      component: () => import('../views/huayitong/huayitong.vue')
    },
    {
      path: 'yishujia',
      component: () => import('../views/yishujia/yishujia.vue'),
      meta:{
        crumb:['古艺术家']
      }
    },
    {
      path: 'yishuadd',
      component: () => import('../views/yishujia/yishujiaAdd.vue'),
      meta:{
        crumb:['古艺术家:/home/yishujia','古艺术家添加']
      }
    },
    {
      path: 'yishuUpdate/:id',
      component: () => import('../views/yishujia/yishujiaUpdate.vue'),
      meta:{
        crumb:['古艺术家:/home/yishujia','古艺术家修改']
      }
    },{
      path: 'yishujia',
      component: () => import('../views/yishujia/yishujia.vue')
    },{
      path: 'wenwu',
      component: () => import('../views/wenwu/wenwu.vue')
    },
    {
      path: 'aisong/add',
      component: () => import('../views/zhuanlan/aisonghuaAdd.vue')
    },
    {
      path: 'aisong/updata/:id',
      component: () => import('../views/zhuanlan/aisonghuaUpdata.vue')
    },{
      path: 'xifang/add',
      component: () => import('../views/zhuanlan/xifangAdd.vue')
    },
    {
      path: 'xifang/upload/:id',
      component: () => import('../views/zhuanlan/xifangupload.vue')
    },
  ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
if(to.path=="/login"){
  next()
}
else{
  if(store.state.user){
    next()
  }
  else{
    next('/login')
 
  }
}
})

export default router
